//
//  YFAlternativeView.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/16.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFAlternativeView.h"

@interface YFAlternativeView()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *firstButton;
@property (weak, nonatomic) IBOutlet UIButton *secondButton;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

@end

@implementation YFAlternativeView

+ (instancetype)alternativeViewWithTitle:(NSString *)title first:(AlternativeItem *)firstItem second:(AlternativeItem *)secondItem currentValue:(kAlternativeSelect)currentSelect {
    YFAlternativeView *alertView = [[[NSBundle mainBundle] loadNibNamed:@"YFAlternativeView" owner:nil options:nil] firstObject];
    alertView.title = title;
    alertView.firstItem = firstItem;
    alertView.secondItem = secondItem;
    alertView.currentIndex = currentSelect;
    return alertView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.text = self.title;
    [_confirmButton clipsCornerRadius:3];
    [self clipsCornerRadius:6 borderWidth:1.5 borderColor:kYellowGreenColor];
    [self configerButton:self.firstButton withItem:self.firstItem];
    [self configerButton:self.secondButton withItem:self.secondItem];
    self.firstButton.selected = (self.currentIndex == kSelectFirst);
    self.secondButton.selected = (self.currentIndex == kSelectSecond);
}

- (void)configerButton:(UIButton *)button withItem:(AlternativeItem *)item {
    [button setTitle:item.selectText forState:UIControlStateSelected];
    [button setTitle:item.unSelectText forState:UIControlStateNormal];
    [button setTitleColor:item.selectTextColor forState:UIControlStateSelected];
    [button setTitleColor:item.unselectTextColor forState:UIControlStateNormal];
    button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
}

- (void)setSelectDotImage:(UIImage *)selectDotImage {
    _selectDotImage = selectDotImage;
    [self.firstButton setImage:selectDotImage forState:UIControlStateSelected];
    [self.secondButton setImage:selectDotImage forState:UIControlStateSelected];
}

- (void)setUnselectDotImage:(UIImage *)unselectDotImage {
    _unselectDotImage = unselectDotImage;
    [self.firstButton setImage:unselectDotImage forState:UIControlStateNormal];
    [self.secondButton setImage:unselectDotImage forState:UIControlStateNormal];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.selectDotImage = [UIImage imageNamed:@"login_select_dot"];
        self.unselectDotImage = [UIImage imageNamed:@"login_unselect_dot"];
    }
    return self;
}

- (IBAction)firstButtonClick:(id)sender {
    self.currentIndex = kSelectFirst;
}

- (IBAction)aecondButtonClick:(id)sender {
    self.currentIndex = kSelectSecond;
}

- (void)setCurrentIndex:(kAlternativeSelect)currentIndex {
    _currentIndex = currentIndex;
    self.firstButton.selected = (self.currentIndex == kSelectFirst);
    self.secondButton.selected = (self.currentIndex == kSelectSecond);
}

- (IBAction)confirmButtonClick:(id)sender {
    if (self.ConfirmButtonBlock) {
        AlternativeItem *item  = self.currentIndex == kUnselect ? nil:(self.currentIndex == kSelectFirst ? self.firstItem:self.secondItem);
        self.ConfirmButtonBlock(self.currentIndex,item);
    }
}

- (void)setConfirmBtnTitle:(NSString *)confirmBtnTitle {
    _confirmBtnTitle = confirmBtnTitle;
    [self.confirmButton setTitle:confirmBtnTitle forState:UIControlStateNormal];
}

@end

@implementation AlternativeItem

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.selectTextColor = kTLightGray999;
        self.unselectTextColor = kTLightGray999;
    }
    return self;
}

+ (instancetype)AlternativeItemWithSelectText:(NSString *)selectText {
    return [self AlternativeItemWithSelectText:selectText unselectText:selectText];
}
+ (instancetype)AlternativeItemWithSelectText:(NSString *)selectText unselectText:(NSString *)unselectText {
    AlternativeItem *item = [AlternativeItem new];
    item.selectText = selectText;
    item.unSelectText = unselectText;
    return item;
}

@end
